<?php
// ini_set("display_errors", "On");
// error_reporting(E_ALL | E_STRICT);
class SogouOpen extends OpenBase
{
    private $redis = null;
    private $scrapy = null;

    public function __construct()
    {
        $this->scrapy = n_api('scrapy');
        $this->redis = n_api('redis')->scrapy;
    }

    /**
     * 提供爬取端访问的cookies
     * @return void {cookie1, cookie2, cookie3...}
     */
    public function generateTaskCookies()
    {
        $data = $this->scrapy->getTaskCookies('sogou');

        $taskCookie = [];
        foreach ($data as $v) {
            $tmp = json_decode($v, true);
            $taskCookie[] = $tmp;
        }

        echo json_encode($taskCookie);
    }

    /**
     * 提供爬取端访问agent
     * @return void {agent1, agent2, agent3...}
     */
  //   public function generateTaskAgents()
  //   {
  //       $param = $this->param('
		// 	taskId:char:empty
		// ');
  //       $taskId = $param['taskId'];

  //       $baseAgents = $this->scrapy->getBrowserBaseAgents();
  //       echo json_encode($baseAgents);
  //   }

    /**
     * 提供爬取端访问Header
     * @return void
     */
    // public function generateTaskHeaders()
    // {
    //     $header = [
    //         'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    //         'Accept-Language' => 'zh-CN,zh;q=0.8',
    //         'Connection' => 'keep-alive',
    //         'Host' => 'weixin.sogou.com',
    //         'Referer' => 'http://weixin.sogou.com/'
    //     ];
    //     echo json_encode($header);
    // }

    public function getWeixinList()
    {
        // $param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
        // $clientIp = $param['clientIp'];
        // $skip = $param['skip'];
        // $limit = $param['limit'];
        
        // if ($skip > 60000) {
        //     echo json_encode([]);
        //     return;
        // }

        // if ($clientIp == '123.125.123.125') {
            $weixinList = n_api('weixin')->getWeixinList($skip, $limit, 1);
            $weixinDisplay = [];

            foreach ($weixinList as $weixinInfo) {
                $tmp['url'] = 'http://weixin.sogou.com/weixin?type=1&query=' . urlencode($weixinInfo['wxname']) . '&ie=utf8';
                $tmp['wxid'] = $weixinInfo['wxid'];

                $weixinDisplay[] = $tmp;
            }
            echo json_encode($weixinDisplay);
        // }
    }

    public function get_weixin_ext_list()
    {
        $param = $this->param('clientIp:char:empty');
        $clientIp = $param['clientIp'];

        $sogou = n_api('sogou');

        $weixinExtFinaList = n_api('weixin')->getWeixinExtList();

        foreach ($weixinExtFinaList as $weixinExtInfo) {
            $urlArr = $sogou->getSogouUrl($weixinExtInfo['sogouid'], $weixinExtInfo['ext']);
            foreach ($urlArr as $url) {
                $tmp['url'] = $url;
                $tmp['wxid'] = $weixinExtInfo['wxid'];
                $tmp['category'] = $weixinExtInfo['category'];

                $result[] = $tmp;
            }
        }
        echo json_encode($result);

        /*$taskLen = 5;
        $key = 'sogou_ext_task_list';

        if ($clientIp == '123.125.123.125') {
            $count = 0;
            $rs = [];
            while (true) {
                $len = $this->redis->lLen($key);
                if ($len != 0 && $count < $taskLen){
                    $tmp = $this->redis->lpop($key);
                    $count++;
                    $rs[] = $tmp;
                }
                else
                    break;
            }
            $display = [];
            foreach ($rs as $value) {
                $display[] = json_decode($value, true);
            }
            echo json_encode($display);
        } else {
            echo json_encode([]);
        }*/
    }
}